/*
 * 描          述:  <描述>
 * 修  改   人:  PengQingyang
 * 修改时间:  2022年4月14日
 * <修改描述:>
 */
package com.tx.component.social.dao.mybatishandler;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.executor.result.ResultMapException;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedTypes;
import org.apache.ibatis.type.TypeHandler;

import com.tx.component.social.context.SocialLoginContext;
import com.tx.component.social.model.SocialAccountType;
import com.tx.core.util.JdbcUtils;

/**
 * 第三方账户类型类型处理器<br/>
 * <功能详细描述>
 * 
 * @author  PengQingyang
 * @version  [版本号, 2022年4月14日]
 * @see  [相关类/方法]
 * @since  [产品/模块版本]
 */
@MappedTypes(value = { SocialAccountType.class })
public class TypeHandler4SocialAccountType
        implements TypeHandler<SocialAccountType> {
    
    /**
     * @param ps
     * @param i
     * @param parameter
     * @param jdbcType
     * @throws SQLException
     */
    @Override
    public void setParameter(PreparedStatement ps, int i,
            SocialAccountType parameter, JdbcType jdbcType)
            throws SQLException {
        if (parameter == null
                && (jdbcType == null || JdbcType.OTHER == jdbcType)) {
            ps.setNull(i, JdbcUtils.getSqlTypeByJavaType(String.class));
        } else {
            if (StringUtils.isEmpty(parameter.getCode())) {
                ps.setNull(i, JdbcUtils.getSqlTypeByJavaType(String.class));
            } else {
                ps.setString(i, parameter.getCode());
            }
        }
    }
    
    /**
     * @param rs
     * @param columnName
     * @return
     * @throws SQLException
     */
    @Override
    public SocialAccountType getResult(ResultSet rs, String columnName)
            throws SQLException {
        try {
            String result = rs.getString(columnName);
            if (StringUtils.isEmpty(result)) {
                return null;
            } else {
                SocialAccountType sat = SocialLoginContext.getContext()
                        .getSocialAccountType(result);
                if (sat == null) {
                    return null;
                } else {
                    return sat;
                }
            }
        } catch (Exception e) {
            throw new ResultMapException("Error attempting to get column '"
                    + columnName + "' from result set.  Cause: " + e, e);
        }
    }
    
    /**
     * @param rs
     * @param columnIndex
     * @return
     * @throws SQLException
     */
    @Override
    public SocialAccountType getResult(ResultSet rs, int columnIndex)
            throws SQLException {
        try {
            String result = rs.getString(columnIndex);
            if (StringUtils.isEmpty(result)) {
                return null;
            } else {
                SocialAccountType sat = SocialLoginContext.getContext()
                        .getSocialAccountType(result);
                if (sat == null) {
                    return null;
                } else {
                    return sat;
                }
            }
        } catch (Exception e) {
            throw new ResultMapException("Error attempting to get column #"
                    + columnIndex + " from result set.  Cause: " + e, e);
        }
    }
    
    /**
     * @param cs
     * @param columnIndex
     * @return
     * @throws SQLException
     */
    @Override
    public SocialAccountType getResult(CallableStatement cs, int columnIndex)
            throws SQLException {
        try {
            String result = cs.getString(columnIndex);
            if (StringUtils.isEmpty(result)) {
                return null;
            } else {
                SocialAccountType sat = SocialLoginContext.getContext()
                        .getSocialAccountType(result);
                if (sat == null) {
                    return null;
                } else {
                    return sat;
                }
            }
        } catch (Exception e) {
            throw new ResultMapException("Error attempting to get column #"
                    + columnIndex + " from callable statement.  Cause: " + e,
                    e);
        }
    }
    
}
